//
//  QJKDCategorySelectView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/3/1.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJKDCategorySelectView.h"
//数据为空
#import "UIScrollView+EmptyDataSet.h"
#import "QJKDCategoryCell.h"
@interface QJKDCategorySelectView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) UICollectionView *selectCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@end

@implementation QJKDCategorySelectView
- (void)setupSelectUIAction{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //大小
    layout.itemSize = CGSizeMake((UISCREEN_WIDTH - 40) / 3.0, 40);
    //最小行间距
    layout.minimumLineSpacing = 5;
    //最小列间距
    layout.minimumInteritemSpacing = 0;
    //滚动方向(默认垂直)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.selectCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH - 40,  260) collectionViewLayout:layout];
    self.selectCollectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView addSubview:self.selectCollectionView];
    self.selectCollectionView.delegate = self;
    self.selectCollectionView.dataSource = self;
    self.selectCollectionView.emptyDataSetDelegate = self;
    self.selectCollectionView.emptyDataSetSource = self;
    //在cell的个数不多的时候也可以竖直滑动
    self.selectCollectionView.alwaysBounceVertical = YES;
    [self.selectCollectionView registerNib:[UINib nibWithNibName:@"QJKDCategoryCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    [self dataRequestAction];
}
#pragma mark -- 数据请求方法
- (void)dataRequestAction{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"0" forKey:@"parent_id"];
    
    [WWNetRequest fetchShopTypeWithParameters:dict success:^(id response) {
        NSArray *dataArr = response[@"data"];

        NSMutableArray *array = [NSMutableArray array];
        
        for (NSDictionary *dict in dataArr) {
            CategoryLeftModel *model = [[CategoryLeftModel alloc]initWithDictionary:dict error:nil];
            [array addObject:model];
        }
//        if (response) {
//
//            for (NSDictionary *dict in dataArr) {
//                NearCategoryModel *model = [[NearCategoryModeldictalloc]initWithDictionary:obj error:nil];
//                [array addObject:model];
//            }
//        }
        self.dataSourceArray = array;
        [self.selectCollectionView reloadData];
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma mark -- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QJKDCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    CategoryLeftModel *model = self.dataSourceArray[indexPath.row];
    [cell cellUpdateWithModel:model];
    return cell;
}
#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryLeftModel *model = self.dataSourceArray[indexPath.row];
    if (self.categoryIDSelectBlock) {
        self.categoryIDSelectBlock(model);
    }
}
#pragma mark -- DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"noDataImage"];
}
#pragma mark -- DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
- (IBAction)cancelBtnClickAction:(UIButton *)sender {
    if (self.cancelBtnClickBlock) {
        self.cancelBtnClickBlock();
    }
}

#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
